from flask import Blueprint, request,jsonify
from extensions import db
import requests
import traceback
from app.voc.model.models_voc import Userinfo
from sqlalchemy.sql import text
from sqlalchemy import or_, func

api = Blueprint('voc/loginAPI', __name__)


@api.route('/', methods=['GET'])
# @login_required
def DD():
    # maxvocId = db.session.query(Dvoc.test).first()
    # result = db.session.execute(text("select * from a"))
    # for i in result:
    #     print(i)
    # return f'欢迎来到我的主页！!{maxvocId}'
    meta = {'status': 200, 'msg': f'欢迎来到我的主页！!123'}
    return jsonify(meta=meta, data=f'欢迎来到我的主页！!')


@api.route('/login', methods=['POST'])
# @login_required
def login():
    res = request.json
    print(res)
    result = {
        "data": {
            "token": "token-admin"
        },
        "code": 0,
        "message": "登录成功"
    }
    return jsonify(result)
    # return responseDelete("测试用户成功", {'data': 33})


# {"code":0,"data":{"username":"admin","roles":["admin"]},"message":"获取用户详情成功"}


@api.route('/info', methods=['GET'])
def info():
    result = {"code":0,"data":{"username":"admin","roles":["admin"]},"message":"获取用户详情成功"}
    return jsonify(result)


# 查  只要返回值如下面的结构即可
'''
{
  "code": 0,
  "data": {
    "list": [
      {
        "createTime": "2024-12-05",
        "email": "2@3",
        "id": "2",
        "phone": "321",
        "roles": "admin",
        "status": false,
        "username": "222"
      },
      {
        "createTime": "2023-07-10",
        "email": "123123",
        "id": "3",
        "phone": "232",
        "roles": "user",
        "status": true,
        "username": "阿萨德"
      }
    ],
    "total": 1
  },
  "message": "获取表格数据成功"
}
'''
@api.route('/table', methods=['GET'])
def gettable():
    res = request.args
    # print(res.get('username'))
    username = res.get('username')
    phone = res.get('phone')
    uinfo = db.session.query(Userinfo)
    if username:
        uinfo = uinfo.filter(Userinfo.username.like(f'%{username}%'))
    if phone:
        uinfo = uinfo.filter(Userinfo.phone.like(f'%{phone}%'))
    uinfo = uinfo.all()
    # print(maxvocId)
    rlist = []
    for i in uinfo:
        dic = {"id":i.id,"email":i.email,"phone":i.phone,"roles":i.roles,"status":i.status,"username":i.username,"createTime":i.createTime}
        rlist.append(dic)

    result = {"code": 0,"data":{"total":1,"list":rlist},"message":"获取表格数据成功"}
    return jsonify(result)

# 增
@api.route('/addTable', methods=['POST'])
def addTable():
    res = request.json
    username = res.get('username')
    phone = res.get('phone')
    email = res.get('email')
    maxvocId = db.session.query(func.max(Userinfo.id)).first()[0]
    useradd = Userinfo(email=email, id=int(maxvocId)+1, phone=phone, roles='user', createTime='2023-07-10',username=username,
                            status=True)
    db.session.add(useradd)
    db.session.commit()
    result = {"code": 0,"message":"添加数据成功"}
    return jsonify(result)

# 改
@api.route('/updateTable', methods=['POST'])
def updateTable():
    res = request.json
    username = res.get('username')
    phone = res.get('phone')
    email = res.get('email')
    id = res.get('id')
    print(11111)
    print(res)
    db.session.query(Userinfo).filter(Userinfo.id == id).update(
            {"username": username,  "email": email, "phone": phone})
    db.session.commit()
    result = {"code": 0,"message":"添加数据成功"}
    return jsonify(result)

# 删
@api.route('/deleteTable', methods=['POST'])
def deleteTable():
    res = request.json
    db.session.query(Userinfo).filter(Userinfo.id == str(res)).delete()
    db.session.commit()
    result = {"code": 0,"message":"添加数据成功"}
    return jsonify(result)



@api.route('/test1', methods=['GET'])
def test1():
    testunfo = 'abcd1234'
    result = {"code": 0,"data":{"test":testunfo},"message":"测试成功"}
    return jsonify(result)